<?php
/**
 * 获取角色列表
 * User: Administrator
 * Date: 2018\8\31 0031
 * Time: 17:12
 */

namespace app\Lib\Model\Role\RoleManage;


use app\Lib\InterfaceIn\Role\RoleManage\GetRoleListHandle;
use app\Lib\Model\DbModel\Role;

class GetRoleList implements GetRoleListHandle
{
    private $keyword;
    private $keywordType;
    private $roleDb;
    private $limit = 10;
    private $page = 1;
    private $isPage;
    /**
     * 查询内容
     * @param $keyword
     * @return $this
     */
    public function setKeyword($keyword)
    {
       $this->keyword = $keyword;
       return $this;
    }

    /**
     * 查询内容类型
     * @param $keywordType
     * @return $this
     */
    public function setKeywordType($keywordType)
    {
        $this->keywordType = $keywordType;
        return $this;
    }

    /**
     * page页数
     * @param $page
     * @return $this
     */
    public function setPage($page =1)
    {
        $this->page = $page;
        return $this;
    }

    /**
     * page页数
     * @param $limit
     * @return $this
     */
    public function setLimit($limit =  10)
    {
        $this->limit = $limit;
        return $this;
    }

    /**
     * isPage获取page
     * @param $isPage
     * @return mixed
     */
    public function isPage($isPage)
    {
        $this->isPage = $isPage;
        return true;
    }

    /**
     * 列表
     * @return array | string
     */
    public function getList()
    {
        $db    =  $this->getRoleDb();
        $map   =  $this->getMap();
        $count = $db->where($map)->count();
        if($this->isPage){
            return $count;
        }
        if($count<0){
            return [];
        }
        $result =  $db->where($map)->limit(($this->page-1)*$this->limit,$this->limit)->column($db->_id);
        $result =  $this->handleArray($result);
        return ['list'=>$result];

    }

    /**
     * 获取查询条件
     * @return array
     */
    private function getMap(){
        $map = [];
        $roleDb =  $this->getRoleDb();
        if($this->keywordType){
            switch ($this->keywordType){
                case 'name':
                    $map[$roleDb->_name] = $this->keyword;
                    break;
            }
        }
        return $map;
    }

    /**
     * 获取数据库
     * @return Role
     */
    private function getRoleDb(){
        if(empty($this->roleDb)){
            $this->roleDb = new Role();
        }
        return $this->roleDb;
    }

    /**
     * 重新获取数组
     * @param $result
     * @return array
     */
    private function handleArray($result){
        $data = [];
        $db = $this->getRoleDb();
        foreach ($result as $k=>$v){
            $model = new \app\Lib\Model\Role\Role($v);
            $saveTime = $model->getSaveTime();
            $saveTime = $saveTime==0?'':$saveTime;
            $parentClass = ($model->getParentClass());
            $parentName = empty($parentClass)?'顶级':$parentClass->getName();
            $array = [
                $db->_id=>$model->getRoleId(),
                $db->_level=>$model->getLevel(),
                $db->_name=>$model->getName(),
                'parentName'=>$parentName,
                'createName'=>$model->getCreateName(),
                'createTime'=>date('Y-m-d H:i:s',$model->getCreateTime()),
                'saveName'=>$model->getSaveUserName(),
                'saveTime'=>empty($saveTime)?'':date('Y-m-d H:i:s',$saveTime),
                $db->_role_alias=>$model->getRoleAlias(),
            ];
            $data[$k]=$array;
        }
        return $data;
    }
}